﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Using MEF as primary IoC container</title>
  <keywords>
    <keyword translate="true">Inversion of Control</keyword>
    <keyword translate="true">IoC</keyword>
    <keyword translate="true">MEF</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Using MEF as primary IoC container</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">If you are going for Catel, we always recommend to use the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> of Catel as primary IoC container. However, if you want to use MEF as the primary IoC container, follow the instructions below.</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">This is not a full starting guide to MEF, if you are looking for documentation about MEF, continue searching</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering the container in Catel</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">It is required to register the external container in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> of Catel. This is required because all the items that are registered by Catel itself are known in the MEF container. Catel will automatically register all types in the MEF container when the container is registered. To register the container, simple use this call when the container is created:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.RegisterExternalContainer(myMefContainer);</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">At this point, all the services registered by Catel are now registered in the MEF container.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Importing properties</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To import services on a class, decorate the properties </text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[Import]</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> IPleaseWaitService PleaseWaitServiceViaMEF { get; set; }</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The code above imports the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Services_IPleaseWaitService" styleclass="Normal" translate="true">IPleaseWaitService</link><text styleclass="Normal" translate="true"> (which is internally registered by Catel) via MEF.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Satisfying imports</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">After all imports are added, it is important to satisfy the imports using MEF. The imports can be satisfied via the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">myMefContainer.SatisfyImportsOnce(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">this</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">);</text></para>
  </body>
</topic>
